package kr.co.shoppingmall.admin;

import java.io.File;
import java.util.HashMap;
import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.commons.CommonsMultipartFile;

@Controller
@RequestMapping("/admin")
public class AdminController {
    @Resource
    private AdminService adminService;

    @RequestMapping("/writeform.htm")
    public String writeform() {
	return "/admin/writeform";
    }

    @RequestMapping("/write.htm")
    public String write(ProductDTO dto, HttpServletRequest request) {
	CommonsMultipartFile file = dto.getFile();
	String fileName = file.getOriginalFilename();
	String path = "D:/Dev/project/shoppingmall/WebContent/images/";
	File saveFile = new File(path + fileName);
	try {
	    file.transferTo(saveFile);
	} catch (Exception e) {
	    e.printStackTrace();
	}
	dto.setProdImage(fileName);
	adminService.insertProduct(dto);
	return "redirect:/admin/list.htm";
    }

    @RequestMapping("list.htm")
    public String list(@RequestParam(required = false, defaultValue = "1") int pg, Model model) {
	int rowSize = 10;
	int begin = (pg * rowSize) - (rowSize - 1);
	int end = pg * rowSize;
	HashMap<String, Integer> map = new HashMap<String, Integer>();
	map.put("begin", begin);
	map.put("end", end);
	List<ProductDTO> list = adminService.getProductList(map);
	int rowCount = adminService.getProductCount();
	int linkSize = 10;
	int beginLink = (pg - 1) / rowSize + 1;
	int endLink = (pg - 1) / rowSize * rowSize + linkSize;
	int totalPage = rowCount / rowSize + (rowCount % rowSize == 0 ? 0 : 1);
	if (endLink > totalPage) {
	    endLink = totalPage;
	}
	int max = rowCount - ((pg - 1) * rowSize);
	model.addAttribute("pg", pg);
	model.addAttribute("linkSize", linkSize);
	model.addAttribute("beginLink", beginLink);
	model.addAttribute("endLink", endLink);
	model.addAttribute("totalPage", totalPage);
	model.addAttribute("max", max);
	model.addAttribute("list", list);
	return "/admin/list";
    }

    @RequestMapping("/read.htm")
    public String read(String prodCode, Model model) {
	ProductDTO dto = adminService.getProduct(prodCode);
	model.addAttribute("p", dto);
	return "/admin/read";
    }

    @RequestMapping("update.htm")
    public String update(ProductDTO dto) {
	CommonsMultipartFile file = dto.getFile();
	String fileName = file.getOriginalFilename();
	if (fileName.equals("")) {
	    String path = "D:/Dev/project/project1/WebContent/images/";
	    File saveFile = new File(path + fileName);
	    try {
		file.transferTo(saveFile);
	    } catch (Exception e) {
		e.printStackTrace();
	    }
	    dto.setProdImage(fileName);
	}
	adminService.updateProduct(dto);
	return "redirect:/admin/list.htm";
    }

    @RequestMapping("delete.htm")
    public String delete(String prodCode) {
	adminService.deleteProduct(prodCode);
	return "redirect:/admin/list.htm";
    }
}
